window.onload = function () {
  // 全选
  var checkAll = document.querySelector('.select-all')
  // 店铺全选
  var allShop = document.querySelectorAll('.shop-info>input')
  var deleteCheck = document.querySelector('.select-all-footer')


  function allCheck() {
    let allShop = document.querySelectorAll('.shop-info>input')
    for (let i = 0; i < allShop.length; i++) {
      allShop[i].checked = deleteCheck.checked = checkAll.checked = this.checked
      var checkitem = allShop[i].parentElement.nextElementSibling.querySelectorAll('.select-logo>input')
      for (var m = 0; m < checkitem.length; m++) {
        checkitem[m].checked = allShop[i].checked
      }
    }
    totall()
  }
  checkAll.addEventListener('change', allCheck)
  deleteCheck.addEventListener('change', allCheck)
  // 给每一个店铺添加input改变事件
  // 店铺添加点击事件
  for (let i = 0; i < allShop.length; i++) {
    allShop[i].addEventListener('change', function () {
      var checkitem = this.parentElement.nextElementSibling.querySelectorAll('.select-logo>input')
      // console.log(checkitem)
      for (let j = 0; j < checkitem.length; j++) {
        checkitem[j].checked = this.checked
      }
      var flag = true
      for (let m = 0; m < allShop.length; m++) {
        // console.log(allShop[m].checked)
        if (!allShop[m].checked) {
          flag = false
        }
      }
      // console.log(flag)
      deleteCheck.checked = checkAll.checked = flag ? true : false
      totall()
    })
    // 给每一个店铺的商品添加删除点击事件
    let checkitem = allShop[i].parentElement.nextElementSibling.querySelectorAll('.select-logo>input')
    let deleteitem = allShop[i].parentElement.nextElementSibling.querySelectorAll('.delete>a')
    for (let n = 0; n < checkitem.length; n++) {
      // 给每一个店铺的商品添加点击事件
      checkitem[n].addEventListener('change', function () {
        var flag1 = true
        // console.log(checkitem)
        for (var x = 0; x < checkitem.length; x++) {
          if (!checkitem[x].checked) {
            flag1 = false
          }
        }
        var thisallshop = this.parentElement.parentElement.parentElement.previousElementSibling.querySelector('input')
        thisallshop.checked = flag1 ? true : false
        // shopcheck()
        var flag = true
        for (let m = 0; m < allShop.length; m++) {
          // console.log(allShop[m].checked)
          if (!allShop[m].checked) {
            flag = false
          }
        }
        // console.log(flag)
        deleteCheck.checked = checkAll.checked = flag ? true : false
        totall()
      })
      // 给每一个店铺的商品绑定删除事件
      deleteitem[n].addEventListener('click', function () {
        var num = this.parentElement.parentElement.parentElement.querySelectorAll('.item')
        console.log(num)
        // 如果该店铺商品为0，则清除店铺
        if (num.length == 1) {
          this.parentElement.parentElement.parentElement.previousElementSibling.remove()
        }
        this.parentElement.parentElement.remove()

        totall()
        allGoods()
      })

      // 给每个商品的数量添加点击事件
      let countAddList = checkitem[n].parentElement.parentElement.querySelectorAll('.count>span')
      // console.log(countAddList)
      // for (let d = 0; d < countAddList.length; d++) {

      // 绑定减号事件
      countAddList[0].addEventListener('click', function () {
        // 获取输入框的值
        var count = countAddList[1].children[0]
        // 获取一件价钱
        var price = this.parentElement.parentElement.querySelector('.price .real-price').innerHTML
        var price1 = price.slice(1)
        console.log(price1)
        if (Number(count.value) > 0) {
          count.value -= 1
        }
        console.log(price1 * count.value)
        // 获取一个商品的总价钱  赋值给商品总价
        var itemtotal = this.parentElement.parentElement.querySelector('.amount')
        var itemtotal1 = itemtotal.innerHTML.slice(1) * count.value
        itemtotal.innerHTML = `￥${itemtotal1}`

        // 获取一个商品的总价钱  赋值给商品总价
        var itemtotal = this.parentElement.parentElement.querySelector('.amount')
        var itemtotal1 = price1 * count.value
        itemtotal.innerHTML = `￥${itemtotal1}`
        totall()

      })

      // 绑定加号事件
      countAddList[2].addEventListener('click', function () {
        // 获取输入框的值
        var count = countAddList[1].children[0]
        count.value = Number(count.value) + 1
        // 获取一件价钱
        var price = this.parentElement.parentElement.querySelector('.price .real-price').innerHTML
        var price1 = price.slice(1)
        console.log(price1)
        // 获取一个商品的总价钱  赋值给商品总价
        var itemtotal = this.parentElement.parentElement.querySelector('.amount')
        var itemtotal1 = price1 * count.value
        itemtotal.innerHTML = `￥${itemtotal1}`
        totall()

      })
      // }
    }

  }

  // 全选的删除
  var deleteAll = document.querySelector('.operate .left>a')
  deleteAll.addEventListener('click', function () {
    var allShop = document.querySelectorAll('.shop-info>input')
    for (let i = 0; i < allShop.length; i++) {
      console.log(allShop[i])
      let allcheckitem = allShop[i].parentElement.nextElementSibling.querySelectorAll('.select-logo>input')
      for (let j = 0; j < allcheckitem.length; j++) {
        if (allcheckitem[j].checked) {
          allcheckitem[j].parentElement.parentElement.remove()
          // allShop[i].checked = false
        }
      }
      console.log(allShop[i].checked)
      if (allShop[i].checked) {
        // console.log(111)
        allShop[i].parentElement.remove()
      }
    }
    // 点了全选旁边的删除，需要把全选按钮选中状态变为false
    this.previousElementSibling.checked = false
    checkAll.checked = false
    totall()
    allGoods()
  })
  // 计算商品和价钱的总数
  function totall() {
    // 获取商品数量
    var count1 = 0
    var countitem = document.querySelectorAll('.count input')
    for (let b = 0; b < countitem.length; b++) {
      // console.log(countitem[b])
      if (countitem[b].parentElement.parentElement.parentElement.querySelector('.select-logo>input').checked) {
        count1 = Number(count1) + Number(countitem[b].value)
      }
    }
    // 更新总价
    // 获取总价钱
    var total = document.querySelector('.amount span')
    var total1 = 0;
    // console.log(total.innerHTML)
    // 获取全部商品
    var itemgoods = document.querySelectorAll('.item .amount')
    for (let a = 0; a < itemgoods.length; a++) {
      console.log(itemgoods)
      if (itemgoods[a].parentElement.querySelector('.select-logo input').checked) {
        // total1 = total.innerHTML.slice(1)
        console.log(total1)
        var itemtotal1 = itemgoods[a].innerHTML.slice(1)
        console.log(itemtotal1)
        total1 = Number(total1) + Number(itemtotal1)

      }
    }
    // console.log(total1)
    total.innerHTML = `￥${Number(total1)}`
    // 获取商品总数
    document.querySelector('.selected-count').innerHTML = count1

    if (count1 > 0) {
      document.querySelector('.settle').style.backgroundColor = 'red'
    }
    else {
      document.querySelector('.settle').style.backgroundColor = '#b0b0b0'
    }
  }
  // 全部商品的个数
  function allGoods() {
    var items = document.querySelectorAll('.item')
    var countGoods = document.querySelector('.title .count')
    console.log(items.length)
    countGoods.innerHTML = items.length
  }
  allGoods()

}